package com.example.taobaou.ui.fragment;


import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.taobaou.R;
import com.example.taobaou.base.BaseFragment;
import com.example.taobaou.model.domain.IBsaeInfo;
import com.example.taobaou.model.domain.OnSellContent;
import com.example.taobaou.presenter.impl.OnSellPagePresenterImpl;
import com.example.taobaou.presenter.impl.TicketPresenterImpl;
import com.example.taobaou.ui.activity.TicketActivity;
import com.example.taobaou.ui.adapter.OnSellContentAdapter;
import com.example.taobaou.utils.LogUtils;
import com.example.taobaou.utils.PresenterManager;
import com.example.taobaou.utils.SizeUtils;
import com.example.taobaou.utils.TicketUtil;
import com.example.taobaou.utils.TostUtils;
import com.example.taobaou.view.IOnSellPageCallback;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

/**
 * A simple {@link Fragment} subclass.
 */
public class OnSellFragment extends BaseFragment implements IOnSellPageCallback, OnSellContentAdapter.OnSellPageItemClickListener {


    private OnSellPagePresenterImpl mOnSellPagePresenter;
    private RecyclerView mContentRv;
    private OnSellContentAdapter contentAdapter;
    private TwinklingRefreshLayout twinklingRefreshLayout;
    private TextView  onSell;

    public OnSellFragment() {
        // Required empty public constructor
    }


    @Override
    protected int getRootViewResID() {
        return R.layout.fragment_onsell;
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_title_layout,container,false);
    }

    @Override
    protected void initView(View rootView) {
        mContentRv = rootView.findViewById(R.id.on_sell_content_list);
        contentAdapter = new OnSellContentAdapter();
        mContentRv.setLayoutManager(new GridLayoutManager(getContext(), 2));
        mContentRv.setAdapter(contentAdapter);
        mContentRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = SizeUtils.dip2px(getContext(), 2.5f);
                outRect.bottom = SizeUtils.dip2px(getContext(), 2.5f);
                outRect.left = SizeUtils.dip2px(getContext(), 2.5f);
                outRect.right = SizeUtils.dip2px(getContext(), 2.5f);
            }
        });

        twinklingRefreshLayout = rootView.findViewById(R.id.on_sell_refresh);
        twinklingRefreshLayout.setEnableRefresh(false);
        twinklingRefreshLayout.setEnableLoadmore(true);
        twinklingRefreshLayout.setEnableOverScroll(true);

        onSell = rootView.findViewById(R.id.on_sell_bar);
        onSell.setVisibility(View.VISIBLE);
        TextView selected = rootView.findViewById(R.id.selected_bar);
        selected.setVisibility(View.GONE);


    }

    @Override
    protected void initListener() {
        twinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                if (mOnSellPagePresenter != null) {
                    mOnSellPagePresenter.loaderMore();
                }
            }
        });

        contentAdapter.setOnSellPageItemClickListener(this);
    }



    @Override
    protected void initPresenter() {
        mOnSellPagePresenter = PresenterManager.getInstance().getOnSellPagePresenter();
        mOnSellPagePresenter.registerViewCallback(this);
        mOnSellPagePresenter.getOnSellContent();
        LogUtils.d(this, "开始加载数据------------------------------------------>");
    }


    @Override
    protected void release() {
        super.release();
        if (mOnSellPagePresenter != null) {
            mOnSellPagePresenter.unregisiterViewCallback(this);
        }
    }

    @Override
    public void onContentloadeSuccess(OnSellContent result) {
        setState(State.SUCCESS);
        //获得数据
        contentAdapter.setData(result);
    }

    @Override
    public void onMoreLoaded(OnSellContent moreResult) {
        //加载更多数据
        twinklingRefreshLayout.finishLoadmore();
        contentAdapter.onLoadedMore(moreResult);
    }

    @Override
    public void onMoreLoadedError() {
        twinklingRefreshLayout.finishLoadmore();
        TostUtils.showToast("网络异常，请稍后重试！");
    }

    @Override
    public void onMoreLoadedEmpty() {
        twinklingRefreshLayout.finishLoadmore();
        TostUtils.showToast("没有更多宝贝了哟~~~~");
    }

    @Override
    public void onNatworkError() {
        setState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setState(State.LOADING);
    }

    @Override
    public void onEmpty() {
        setState(State.EMPTY);
    }

    @Override
    public void onSellItemClickLisenter(IBsaeInfo item) {
        //特惠内容列表被点击
//
//        String title = item.getTitle();
//        String click_url = item.getCoupon_click_url();
//        if (TextUtils.isEmpty(click_url)) {
//            click_url = item.getClick_url();
//        }
//        String pict_url = item.getPict_url();
//        TicketPresenterImpl ticketPresenter = PresenterManager.getInstance().getTicketPresenter();
//        ticketPresenter.getTicket(title,click_url,pict_url);
//
//        startActivity(new Intent(getContext(), TicketActivity.class));

        TicketUtil.toTicketPage(getContext(),item);


    }
}
